﻿using PayrollHR.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Payroll.BusinessLogic.Messages;
using Payroll.Common.Utils;
namespace PayrollHR.Administration.Modules
{
    public partial class AddEmailAccount : BasePayrollAdministrationUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.Bind();
            }
        }
        private void Bind()
        {
            var item = this.MessageService.GetEmailAccounts();
            EmailInfo.Bind(item);
        }
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    EmailAccounts email = EmailInfo.GetEmail();
                    SaveButton.Enabled = false;
                    email = this.Save();
                    lblloi.Visible = false;
                    Response.Redirect("ConfigEmail.aspx");
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
                finally
                {
                    SaveButton.Enabled = true;
                }
            }
        }
        protected EmailAccounts Save()
        {
            EmailAccounts email = EmailInfo.GetEmail();
            if (CommonHelper.IsValidEmail(email.Email))
            {
                this.MessageService.SaveEmailAccounts(email);
            }
            else
            {
                lblloi.Visible = true;
                lblloi.Text = String.Format("Email {0}  không hợp lệ.", email.Email);
                Response.Redirect("ConfigEmail.aspx");
            }
            return email;
        }
    }
}